﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace CodePlex.Resourceful.Microsoft.Azure
{
    //<QueueMessagesList>
    //  <QueueMessage>
    //    <MessageId>795a36fd-e896-49e5-9de5-9cfc5d99cd57</MessageId>
    //    <InsertionTime>Mon, 08 Jun 2009 01:03:11 GMT</InsertionTime>
    //    <ExpirationTime>Mon, 15 Jun 2009 01:03:11 GMT</ExpirationTime>
    //    <MessageText>myfirstmessage</MessageText>

    //    <PopReceipt>opaque-string-receipt-data</PopReceipt>
    //    <TimeNextVisible>time-next-visible</TimeNextVisible>


    //  </QueueMessage>
    //</QueueMessagesList>
    public class MessageListing
    {
        private readonly List<MessageInfo> _messages;

        public MessageListing(List<MessageInfo> messages)
        {
            _messages = messages;
        }


        public static MessageListing Parse(XmlReader reader)
        {

            List<MessageInfo> messages = new List<MessageInfo>();

            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element)
                {

                    if (reader.LocalName == "QueueMessage")
                    {
                        MessageInfo message = MessageInfo.Parse(reader);
                        messages.Add(message);
                    }
                }

                if (reader.LocalName == "QueueMessagesList" && (reader.NodeType == XmlNodeType.EndElement || reader.IsEmptyElement))
                {
                    return new MessageListing(messages);
                }

            }

            throw CodePlexException.Format("Failed to parse QueueMessagesList.");
        }

        public IEnumerable<MessageInfo> Messages { get { return _messages; } }

    }

    public class MessageInfo
    {
        private readonly string _messageID;
        private readonly string _insertionTime;
        private readonly string _expirationTime;
        private readonly string _messageText;

        private readonly string _popReceipt;
        private readonly string _timeNextVisible;

        public MessageInfo(string messageID, string insertionTime, string expirationTime, string messageText, string popReceipt, string timeNextVisible)
        {
            _messageID = messageID;
            _insertionTime = insertionTime;
            _expirationTime = expirationTime;
            _messageText = messageText;

            _popReceipt = popReceipt;
            _timeNextVisible = timeNextVisible;

        }

        public string MessageID
        {
            get
            {
                return _messageID;
            }
        }
        public string InsertionTime
        {
            get
            {
                return _insertionTime;
            }
        }
        public string ExpirationTime
        {
            get
            {
                return _expirationTime;
            }
        }
        public string MessageText
        {
            get
            {
                return _messageText;
            }
        }

        public string PopReceipt
        {
            get
            {
                return _popReceipt;
            }
        }

        public string TimeNextVisible
        {
            get
            {
                return _timeNextVisible;
            }
        }


        //    <MessageId>795a36fd-e896-49e5-9de5-9cfc5d99cd57</MessageId>
        //    <InsertionTime>Mon, 08 Jun 2009 01:03:11 GMT</InsertionTime>
        //    <ExpirationTime>Mon, 15 Jun 2009 01:03:11 GMT</ExpirationTime>
        //    <MessageText>myfirstmessage</MessageText>
        //    <PopReceipt>opaque-string-receipt-data</PopReceipt>
        //    <TimeNextVisible>time-next-visible</TimeNextVisible>
        public static MessageInfo Parse(XmlReader reader)
        {


            string messageID = default(string);
            string insertionTime = default(string);
            string expirationTime = default(string);
            string messageText = default(string);
            string popReceipt = default(string);
            string timeNextVisible = default(string);


            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element)
                {
                    if (reader.LocalName == "MessageId" && !reader.IsEmptyElement)
                    {
                        messageID = Utils.ReadString(reader);
                    }
                    if (reader.LocalName == "InsertionTime" && !reader.IsEmptyElement)
                    {
                        insertionTime = Utils.ReadString(reader);
                    }
                    if (reader.LocalName == "ExpirationTime" && !reader.IsEmptyElement)
                    {
                        expirationTime = Utils.ReadString(reader);
                    }
                    if (reader.LocalName == "MessageText" && !reader.IsEmptyElement)
                    {
                        messageText = Utils.ReadString(reader);
                    }
                    if (reader.LocalName == "PopReceipt" && !reader.IsEmptyElement)
                    {
                        popReceipt = Utils.ReadString(reader);
                    }
                    if (reader.LocalName == "TimeNextVisible" && !reader.IsEmptyElement)
                    {
                        timeNextVisible = Utils.ReadString(reader);
                    }

                }

                if (reader.NodeType == XmlNodeType.EndElement && reader.LocalName == "QueueMessage")
                {
                    return new MessageInfo(messageID, insertionTime, expirationTime, messageText, popReceipt, timeNextVisible);
                }

            }

            throw CodePlexException.Format("Failed to parse QueueMessage.");
        }



    }








    //<EnumerationResults AccountName="https://john.queue.core.windows.net/">
    //  <Prefix>m</Prefix>
    //  <Marker>/john/mysecondqueue</Marker>
    //  <MaxResults>4</MaxResults>
    //  <Queues>
    //    <Queue>
    //      <QueueName>myfirstqueue</QueueName>
    //      <Url>https://john.queue.core.windows.net/myfirstqueue</Url>
    //    </Queue>
    //  </Queues>
    //  <NextMarker>/john/mysecondqueue</NextMarker>
    //</EnumerationResults>
    public class QueueListing
    {
        private readonly Uri _accountName;

        private readonly string _prefix;
        private readonly string _marker;
        private readonly int? _maxResults;


        private readonly List<QueueInfo> _queues;


        private readonly string _nextMarker;


        public QueueListing(Uri accountName, string prefix, string marker, int? maxResults,
            IEnumerable<QueueInfo> queues, string nextMarker)
        {
            _accountName = accountName;
            _prefix = prefix;
            _marker = marker;
            _maxResults = maxResults;

            _queues = new List<QueueInfo>(queues);

            _nextMarker = nextMarker;
        }


        public static QueueListing Parse(XmlReader reader)
        {
            Uri accountName = default(Uri);
            string prefix = default(string);
            string marker = default(string);
            int? maxResults = default(int?);

            List<QueueInfo> queues = new List<QueueInfo>();
            string nextMarker = default(string);

            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element)
                {
                    if (reader.LocalName == "EnumerationResults")
                    {
                        accountName = new Uri(reader.GetAttribute("AccountName"));
                    }
                    if (reader.LocalName == "Prefix" && !reader.IsEmptyElement)
                    {
                        prefix = Utils.ReadString(reader);
                    }
                    if (reader.LocalName == "Marker" && !reader.IsEmptyElement)
                    {
                        marker = Utils.ReadString(reader);
                    }
                    if (reader.LocalName == "MaxResults" && !reader.IsEmptyElement)
                    {
                        maxResults = int.Parse(Utils.ReadString(reader));
                    }

                    if (reader.LocalName == "Queue")
                    {
                        QueueInfo queue = QueueInfo.Parse(reader);
                        queues.Add(queue);
                    }

                    if (reader.LocalName == "NextMarker" && !reader.IsEmptyElement)
                    {
                        nextMarker = Utils.ReadString(reader);
                    }

                }

                if (reader.NodeType == XmlNodeType.EndElement && reader.LocalName == "EnumerationResults")
                {
                    return new QueueListing(accountName, prefix, marker, maxResults, queues, nextMarker);
                }

            }

            throw CodePlexException.Format("Failed to parse EnumerationResults.");
        }



        public Uri AccountName { get { return _accountName; } }
        public string Prefix { get { return _prefix; } }
        public string Marker { get { return _marker; } }
        public int? MaxResults { get { return _maxResults; } }


        public IEnumerable<QueueInfo> Queues { get { return _queues; } }

        public string NextMarker { get { return _nextMarker; } }

        public bool IsEmpty { get { return _queues.Count == 0; } }
    }





    public class QueueInfo
    {

        private readonly Uri _queueUri;
        private readonly string _queueName;

        public QueueInfo(Uri queueUri, string queueName)
        {
            _queueUri = queueUri;
            _queueName = queueName;

        }



        public Uri QueueUri
        {
            get
            {
                return _queueUri;
            }
        }
        public string QueueName
        {
            get
            {
                return _queueName;
            }
        }


        //      <QueueName>myfirstqueue</QueueName>
        //      <Url>https://john.queue.core.windows.net/myfirstqueue</Url>
        public static QueueInfo Parse(XmlReader reader)
        {

            Uri queueUri = default(Uri);
            string queueName = default(string);

            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element)
                {

                    if (reader.LocalName == "Url" && !reader.IsEmptyElement)
                    {
                        queueUri = new Uri(Utils.ReadString(reader));
                    }
                    if (reader.LocalName == "QueueName" && !reader.IsEmptyElement)
                    {
                        queueName = Utils.ReadString(reader);
                    }

                }

                if (reader.NodeType == XmlNodeType.EndElement && reader.LocalName == "Queue")
                {
                    return new QueueInfo(queueUri, queueName);
                }

            }

            throw CodePlexException.Format("Failed to parse Queue.");
        }



    }


}
